package cn.com.surker.material.controller;

import cn.com.surker.common.AbstractController;
import cn.com.surker.dto.main.MainMaterialLimitDTO;
import cn.com.surker.material.entity.MainMaterial;
import cn.com.surker.material.service.MainMaterialLimitService;
import cn.com.surker.material.service.MainMaterialService;
import cn.com.surker.material.service.MainMaterialDiscountService;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 材料限量表 控制层
 */
@RestController
@RequestMapping("/mainmateriallimit")
public class MainMaterialLimitController extends AbstractController {


    @Resource
    private MainMaterialLimitService mainMaterialLimitService;

    @Resource
    private MainMaterialDiscountService mainMaterialDiscountService;

    @Resource
    private MainMaterialService mainMaterialService;

    /**
     * 材料限量表 分页功能
     * @param dto
     * @return
     */
    @PostMapping(value = "/page")
    public RestResultDto findPage(@RequestBody MainMaterialLimitDTO dto){
        return RestResultDto.success(mainMaterialLimitService.findPage(dto));
    }

    /**
     * 材料限量表 新增功能
     * @param dto
     * @return
     */
    @PostMapping(value = "/save")
    public RestResultDto saveMaterialLimit(@RequestBody MainMaterialLimitDTO dto){
        CacheUser user = getUser();
        int resultVal = mainMaterialLimitService.saveMaterialLimit(dto, user);
        if(resultVal > 0){
            return RestResultDto.success("新增成功");
        }
        return RestResultDto.fail("新增失败！");
    }

    /**
     * 材料限量表 编辑功能
     * @param dto
     * @return
     */
    @PostMapping(value = "/update")
    public RestResultDto update(@RequestBody MainMaterialLimitDTO dto){
        CacheUser user = getUser();
        int resultVal = mainMaterialLimitService.updateMaterialLimit(dto, user);
        if(resultVal > 0){
            return RestResultDto.success("更新成功");
        }
        return RestResultDto.fail("更新失败！");
    }


    /**
     * 材料限量表 状态更新功能
     * @param dto
     * @return
     */
    @PostMapping(value = "/updatestatus")
    public RestResultDto updateStatus(@RequestBody MainMaterialLimitDTO dto){
        CacheUser user = getUser();
        int resultVal = mainMaterialLimitService.updateStatus(dto.getId(),dto.getStatus(), user);
        if(resultVal > 0){
            return RestResultDto.success("更新成功");
        }
        return RestResultDto.fail("更新失败！");
    }

    /**
     * 获取单位关联的供应商信息
     * @param dto
     * @return
     */
    @PostMapping(value = "/getSupplierList")
    public RestResultDto getSupplierListByOrgId(@RequestBody MainMaterialLimitDTO dto){
        List<Map<String, Object>> supplierListByOrgId = mainMaterialDiscountService.getSupplierListByOrgId(dto.getOrgId().toString());
        return RestResultDto.success(supplierListByOrgId);
    }

    /**
     * 获取单位关联的材料信息
     * @param dto
     * @return
     */
    @PostMapping(value = "/getMaterialByOrgId")
    public RestResultDto getMaterialByOrgId(@RequestBody MainMaterialLimitDTO dto){
        List<MainMaterial> listMaterialByOrgId = mainMaterialService.getListMaterialByOrgId(dto.getOrgId());
        return RestResultDto.success(listMaterialByOrgId);
    }

}
